package com.example.demo.common.controller;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.entity.Inclusion;
import com.example.demo.common.entity.Micro;
import com.example.demo.common.mapper.MicroMapper;
import com.example.demo.common.service.MicroService;
import com.example.demo.common.utils.Compress;
import com.example.demo.common.utils.IpUtil;
import org.springframework.web.bind.annotation.*;
//import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/micro")
public class MicroController {
    @Resource
    Compress compress;
    @Resource
    MicroService microService;
    @Resource
    MicroMapper microMapper;
    @Resource
    IpUtil ipUtil;
    @PostMapping
    public Result<?> save(@RequestBody Micro micro) throws IOException {
        System.out.println(micro);
        microService.saveMicro(micro);
        microService.updateMicro(getZIP(micro));
        System.out.println(micro);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody Micro micro){
        microService.updateMicro(micro);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        microService.deleteById(id);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Micro> wrapper = Wrappers.<Micro>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Micro::getRef, search);
        }
        Page<Micro> MicroPage = microMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);


        return Result.success(MicroPage);
    }
    @RequestMapping("/fix")
    public Result<?> findSampId(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") Long sampId){
        LambdaQueryWrapper<Micro> wrapper = Wrappers.<Micro>lambdaQuery();

        System.out.println(sampId);
        wrapper.eq(Micro::getSampid, sampId);
        Page<Micro> MicroPage = microMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);

        return Result.success(MicroPage);
    }
    public Micro getZIP(Micro micro) throws IOException {
        List<String> sourceFilePaths = new ArrayList<String>();
        sourceFilePaths.add("E:\\test\\1\\"+micro.getEbsdzip());
        sourceFilePaths.add("E:\\test\\1\\"+micro.getEbsdctf());
        sourceFilePaths.add("E:\\test\\1\\"+micro.getSem());
        sourceFilePaths.add("E:\\test\\1\\"+micro.getMet());
        String zipTempFilePath = "E:/test/1/micro/"+micro.getId()+'-'+micro.getRef()+".zip";
        int s = compress.compress1(sourceFilePaths, zipTempFilePath,false);
        micro.setZip("http://10.39.199.216//1/micro/"+micro.getId()+'-'+micro.getRef()+".zip");
        return micro;
    }

}
